package com.adv.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import com.adv.mapper.PPermissionMapper;
import com.adv.mapper.PRoleMapper;
import com.adv.mapper.PRolePermissionLinkMapper;
import com.adv.mapper.PUserRoleLinkMapper;
import com.adv.mapper.UserMapper;
import com.adv.model.PPermission;
import com.adv.model.PPermissionSqlParam;
import com.adv.model.PRole;
import com.adv.model.PRolePermissionLink;
import com.adv.model.PRolePermissionLinkSqlParam;
import com.adv.model.PRoleSqlParam;
import com.adv.util.Constant;
import com.adv.util.DozerUtil;
import com.adv.vo.PermissionForm;
import com.adv.vo.RoleForm;

@SuppressWarnings("unused")
@Component
public class RoleService {

	@Autowired
	private UserMapper adminMapper;

	@Autowired
	private PRoleMapper roleMapper;

	@Autowired
	private PUserRoleLinkMapper userRoleLinkMapper;

	@Autowired
	private PPermissionMapper permissionMapper;

	@Autowired
	private PRolePermissionLinkMapper rolePermissionLinkMapper;

	public List<PRole> getList() {
		return roleMapper.selectByParam(new PRoleSqlParam());
	}

	public RoleForm initForm(RoleForm form) {
		if (null == form) {
			form = new RoleForm();
		}
		PPermissionSqlParam permissionSqlParam = new PPermissionSqlParam();
		List<PPermission> permissioins = permissionMapper.selectByParam(permissionSqlParam);
		form.setPermissions(DozerUtil.mapList(permissioins, PermissionForm.class));
		if (null != form.getId()) {
			PRole role = roleMapper.selectByPrimaryKey(form.getId());
			permissioins = role.getPermissions();
			role.setPermissions(new ArrayList<PPermission>());
			DozerUtil.mapObj(role, form);
			if (!CollectionUtils.isEmpty(permissioins)) {
				for (PPermission permission : permissioins) {
					for (PermissionForm permissionForm : form.getPermissions()) {
						if (permission.getId().equals(permissionForm.getId())) {
							permissionForm.setChecked(true);
							break;
						}
					}
				}
			}
		}
		return form;
	}

	public Map<String, Object> save(RoleForm form) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		resultMap.put(Constant.RESULTKEY, Constant.FAILURE);
		if (null != form) {
			if (StringUtils.isEmpty(form.getAddtime())) {
				form.setAddtime(Constant.SDFCOMMON.format(new Date()));
			}
			PRole record = DozerUtil.mapObj(form, PRole.class);
			if (null != record.getId()) {
				int num = roleMapper.updateByPrimaryKeySelective(record);
				if (num == 1) {
					resultMap.put(Constant.RESULTKEY, Constant.SUCCESS);
				}
			} else {
				int num = roleMapper.insertSelective(record);
				if (num == 1) {
					resultMap.put(Constant.RESULTKEY, Constant.SUCCESS);
				}
			}
			if (Constant.SUCCESS.equals(resultMap.get(Constant.RESULTKEY))) {
				PRolePermissionLinkSqlParam param = new PRolePermissionLinkSqlParam();
				param.createCriteria().andRoleidEqualTo(record.getId());
				rolePermissionLinkMapper.deleteByParam(param);
				if (null != form.getCheckedPermissions()) {
					List<PRolePermissionLink> rolePermissionlinks = new ArrayList<PRolePermissionLink>();
					for (Integer checkedPermissionId : form.getCheckedPermissions()) {
						PRolePermissionLink link = new PRolePermissionLink();
						link.setPermissionid(checkedPermissionId);
						link.setRoleid(record.getId());
						rolePermissionlinks.add(link);
					}
					rolePermissionLinkMapper.insertBatchBySelective(rolePermissionlinks, rolePermissionlinks.get(0));
				}
			}
		}
		return resultMap;
	}

}
